/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica.util;

import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import weka.clusterers.ClusterEvaluation;
import weka.clusterers.SimpleKMeans;
import weka.gui.explorer.ClustererPanel;
import weka.clusterers.XMeans;
import weka.core.Instances;
import weka.gui.visualize.PlotData2D;
import weka.gui.visualize.VisualizePanel;

/**
 *
 * @author Ion B. Padilla
 */
public class DataMiningHelper {
    /**
     * Metodo de apoyo, para visualizar los clusters en una ventana emergente
     */
    public void visualizarCluster(SimpleKMeans xMeans, Instances instancia , ClusterEvaluation eval) throws Exception {
        PlotData2D predData = ClustererPanel.setUpVisualizableInstances(instancia, eval);
        String name = (new SimpleDateFormat("HH:mm:ss - ")).format(new Date());
        String cname = xMeans.getClass().getName();
        if (cname.startsWith("weka.clusterers."))
          name += cname.substring("weka.clusterers.".length());
        else
          name += cname;

        VisualizePanel vp = new VisualizePanel();
        vp.setName(name + " (" + instancia.relationName() + ")");
        predData.setPlotName(name + " (" + instancia.relationName() + ")");
        vp.addPlot(predData);

        // display data
        // taken from: ClustererPanel.visualizeClusterAssignments(VisualizePanel)
        String plotName = vp.getName();
        final javax.swing.JFrame jf =
          new javax.swing.JFrame("Weka Clusterer Visualize: " + plotName);
        jf.setSize(500,400);
        jf.getContentPane().setLayout(new BorderLayout());
        jf.getContentPane().add(vp, BorderLayout.CENTER);
        jf.addWindowListener(new java.awt.event.WindowAdapter() {
          public void windowClosing(java.awt.event.WindowEvent e) {
            jf.dispose();
          }
        });
        jf.setVisible(true);
    }
}
